package yxy.game.pm2.active.command.tengacha;

import yxy.apple.logger.Logger;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actbeforehand;
import yxy.cherry.data.bean.herodata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_十连预抽;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActTenGachaDrawRewardRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;

@MessageType(proto = Command.ActTenGachaDrawReward, classtype = ActiveMessage.ActTenGachaDrawRewardRequest.class)
public class CMD_Active_十连预抽_DrawReward extends Command<ActTenGachaDrawRewardRequest> {

	private static final Logger logger = Logger.create(CMD_Active_十连预抽_DrawReward.class);
	
	@Override
	public void execute(MessageSource messageSource, ActTenGachaDrawRewardRequest request) throws Exception {
		String pcode = messageSource.getTag();
		int type = request.getType();
		
		ActiveMessage.ActTenGachaDrawRewardResponse.Builder response = ActiveMessage.ActTenGachaDrawRewardResponse.newBuilder();
		
		ActiveModule_十连预抽 am = ActiveModule_十连预抽.get(pcode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(!am.getHerogroup().contains(type)) {
			send(messageSource, response, Result.ActCannotGet);
			return;
		}
		
		actbeforehand execl = ActDataCenter.getData(am.getId(), actbeforehand.class, pcode);
		if(execl == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		String taskcode = am.getIdcodemap().get(am.getId());
		Task taskbean = Task.get(pcode, taskcode);
		
		if(taskbean.isReward()) {
			send(messageSource, response, Result.ActHadGet);
			return;
		}
		
		if(!taskbean.isFinish()) {
			send(messageSource, response, Result.ActCannotGet);
			return;
		}
		
		int[] heroids = new int[]{};
		if(type == 1) {
			heroids = execl.getHeroid1();
		}
		if(type == 2) {
			heroids = execl.getHeroid2();
		}
		if(type == 3) {
			heroids = execl.getHeroid3();
		}
		
		ArrayList<Currency> curs = new ArrayList<Currency>();
		for(int heroid : heroids) {
			Currency cur = Currency.create(CurrencyType.CT_武将, heroid, 1);
			curs.add(cur);
		}
		
		Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
		
		am.setGetherotype(type);
		am.set();
		
		am.next();
		
		//下发道具
//		new CurrencyHandle().addCurrency(pcode, CCReason.活动_十连预抽, curs);
//		ArrayList<Hero> addheros = new ArrayList<Hero>();
		for(int heroid : heroids) {
			herodata heroexecl = DataCenter.getData(heroid, herodata.class);
			Hero hero = Hero.create(heroexecl, pcode, CCReason.活动_十连预抽);
		}
		
		response.addAllDisplay(items);
		send(messageSource, response, Result.Success);
	}

	
	private void send(MessageSource messageSource, ActiveMessage.ActTenGachaDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
